package awk.jdk8;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 编写一个程序，开启 3 个线程，这三个线程的 ID 分别为 A、B、C，每个线程将自己的 ID 在屏幕上打印 3 遍，要求输出的结果必须按顺序显示。
 * 如：ABCABCABC…… 依次递归
 * <p>
 * 三个线程逐次执行,使用Condition
 */
public class TestABCAlternate {

    public static void main(String[] args) {
        final AlternateDemo ad = new AlternateDemo();

        new Thread(() -> {
            for (int i = 1; i <= 3; i++) {
                ad.loopA(i);
            }
        }, "A").start();

        new Thread(() -> {
            for (int i = 1; i <= 3; i++) {
                ad.loopB(i);
            }
        }, "B").start();

        new Thread(() -> {
            for (int i = 1; i <= 3; i++) {
                ad.loopC(i);
                System.out.println("-----------------------------------");
            }
        }, "C").start();
    }

    static class AlternateDemo {
        /**
         * 当前正在执行线程的标记
         */
        private int number = 1;
        private Lock lock = new ReentrantLock();
        /**
         * 三个Condition 链式传播,当某一个Condition调用await 方法则阻塞,其他线程执行完后再触发其signal方法,该线程被唤醒继续执行
         */
        private Condition condition1 = lock.newCondition();
        private Condition condition2 = lock.newCondition();
        private Condition condition3 = lock.newCondition();

        /**
         * @param totalLoop : 循环第几轮
         */
        public void loopA(int totalLoop) {
            lock.lock();
            try {
                //1. 判断
                if (number != 1) {
                    condition1.await();
                }

                //2. 打印
                for (int i = 1; i <= 1; i++) {
                    System.out.println(Thread.currentThread().getName() + "\t" + i + "\t" + totalLoop);
                }
                System.out.println("loopA 执行完毕,condition2唤醒,将要执行loopB");
                //3. 唤醒
                number = 2;
                condition2.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }

        public void loopB(int totalLoop) {
            lock.lock();
            try {
                //1. 判断
                if (number != 2) {
                    condition2.await();
                }

                //2. 打印
                for (int i = 1; i <= 1; i++) {
                    System.out.println(Thread.currentThread().getName() + "\t" + i + "\t" + totalLoop);
                }
                System.out.println("loopB 执行完毕,condition3 唤醒,将要执行loopC");
                //3. 唤醒
                number = 3;
                condition3.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }

        public void loopC(int totalLoop) {
            lock.lock();

            try {
                //1. 判断
                if (number != 3) {
                    condition3.await();
                }

                //2. 打印
                for (int i = 1; i <= 1; i++) {
                    System.out.println(Thread.currentThread().getName() + "\t" + i + "\t" + totalLoop);
                }
                System.out.println("loopC 执行完毕,condition1 唤醒,将要执行 loopA");
                //3. 唤醒
                number = 1;
                condition1.signal();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }

    }
}
